﻿using System;
using System.Configuration;
using XrmLibrary.Connections;

namespace XrmLibrary.ConfigFile
{
    public class CrmServiceConfigurationElement : ConfigurationElement
    {
        public static CrmServiceConfigurationElement Instantiate(String configurationName, String crmOrganizationUrl, String crmDiscoveryUrl, AuthenticationType authType, String crmUserName, String crmPassword, String deviceName = null, String devicePassword = null)
        {
            return new CrmServiceConfigurationElement
                       {
                           Name = configurationName,
                           CrmOrganizationUrl = crmOrganizationUrl,
                           CrmDiscoveryUrl = crmDiscoveryUrl,
                           CrmAuthenticationType = authType.ToString("G"),
                           CrmUserName = crmUserName,
                           CrmPassword = crmPassword,
                           DeviceName = deviceName,
                           DevicePassword = devicePassword
                       };
        }

        private const String ConfigurationName = "name";
        [ConfigurationProperty(ConfigurationName, DefaultValue="", IsKey=false, IsRequired=false)]
        public String Name
        {
            get { return (String) base[ConfigurationName]; }
            set { base[ConfigurationName] = value; }
        }

        private const String CrmOrganizationUrlName = "crmOrganizationUrl";
        [ConfigurationProperty(CrmOrganizationUrlName, DefaultValue="", IsKey=true, IsRequired=true)]
        public String CrmOrganizationUrl
        {
            get { return (String) base[CrmOrganizationUrlName]; }
            set { base[CrmOrganizationUrlName] = value; }
        }

        private const String CrmDiscoveryUrlName = "crmDiscoveryUrl";
        [ConfigurationProperty(CrmDiscoveryUrlName, DefaultValue="", IsKey = false, IsRequired = false)]
        public String CrmDiscoveryUrl
        {
            get { return (String)base[CrmDiscoveryUrlName]; }
            set { base[CrmDiscoveryUrlName] = value; }
        }

        private const String CrmAuthenticationTypeName = "crmAuthenticationType";
        [ConfigurationProperty(CrmAuthenticationTypeName, DefaultValue = "", IsKey = false, IsRequired = true)]
        public String CrmAuthenticationType
        {
            get { return (String)base[CrmAuthenticationTypeName]; }
            set { base[CrmAuthenticationTypeName] = value; }
        }

        private const String CrmUserNameName = "crmUserName";
        [ConfigurationProperty(CrmUserNameName, DefaultValue = "", IsKey = false, IsRequired = false)]
        public String CrmUserName
        {
            get { return (String)base[CrmUserNameName]; }
            set { base[CrmUserNameName] = value; }
        }

        private const String CrmPasswordName = "crmPassword";
        [ConfigurationProperty(CrmPasswordName, DefaultValue = "", IsKey = false, IsRequired = false)]
        public String CrmPassword
        {
            get { return (String)base[CrmPasswordName]; }
            set { base[CrmPasswordName] = value; }
        }


        private const String DeviceNameName = "deviceName";
        [ConfigurationProperty(DeviceNameName, DefaultValue = "", IsKey = false, IsRequired = false)]
        public String DeviceName
        {
            get { return (String)base[DeviceNameName]; }
            set { base[DeviceNameName] = value; }
        }

        private const String DevicePasswordName = "devicePassword";
        [ConfigurationProperty(DevicePasswordName, DefaultValue = "", IsKey = false, IsRequired = false)]
        public String DevicePassword
        {
            get { return (String)base[DevicePasswordName]; }
            set { base[DevicePasswordName] = value; }
        }
    }
}
